package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.MaterialInventory;
import com.ruoyi.factorydata.domain.StockOutOrderDetail;
import com.ruoyi.factorydata.mapper.MaterialInventoryMapper;
import com.ruoyi.factorydata.mapper.StockOutOrderDetailMapper;
import com.ruoyi.factorydata.service.IStockOutOrderDetailService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 出库单详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-26
 */
@Service
public class StockOutOrderDetailServiceImpl extends ServiceImpl<StockOutOrderDetailMapper, StockOutOrderDetail> implements IStockOutOrderDetailService {

    private final StockOutOrderDetailMapper stockOutOrderDetailMapper;

    private final MaterialInventoryMapper materialInventoryMapper;

    public StockOutOrderDetailServiceImpl(StockOutOrderDetailMapper stockOutOrderDetailMapper, MaterialInventoryMapper materialInventoryMapper) {
        this.stockOutOrderDetailMapper = stockOutOrderDetailMapper;
        this.materialInventoryMapper = materialInventoryMapper;
    }

    /**
     * 查询出库单详情
     *
     * @param id 出库单详情主键
     * @return 出库单详情
     */
    @Override
    public StockOutOrderDetail selectStockOutOrderDetailById(Long id) {
        return stockOutOrderDetailMapper.selectStockOutOrderDetailById(id);
    }

    /**
     * 查询出库单详情列表
     *
     * @param stockOutOrderDetail 出库单详情
     * @return 出库单详情
     */
    @Override
    public List<StockOutOrderDetail> selectStockOutOrderDetailList(StockOutOrderDetail stockOutOrderDetail) {
        List<StockOutOrderDetail> stockOutOrderDetails = stockOutOrderDetailMapper.selectStockOutOrderDetailList(stockOutOrderDetail);
        stockOutOrderDetails.forEach(detail -> {
            Long stockQuantity = materialInventoryMapper.countMaterialInventoryList(MaterialInventory.builder().materialId(detail.getMaterialId()).build());
            stockQuantity= Optional.ofNullable(stockQuantity).orElse(0L);
            detail.setStockQuantity(stockQuantity);
        });
        return stockOutOrderDetails;
    }

    /**
     * 新增出库单详情
     *
     * @param stockOutOrderDetail 出库单详情
     * @return 结果
     */
    @Override
    public int insertStockOutOrderDetail(StockOutOrderDetail stockOutOrderDetail) {
                stockOutOrderDetail.setCreateTime(DateUtils.getNowDate());
            return stockOutOrderDetailMapper.insertStockOutOrderDetail(stockOutOrderDetail);
    }

    /**
     * 修改出库单详情
     *
     * @param stockOutOrderDetail 出库单详情
     * @return 结果
     */
    @Override
    public int updateStockOutOrderDetail(StockOutOrderDetail stockOutOrderDetail) {
                stockOutOrderDetail.setUpdateTime(DateUtils.getNowDate());
        return stockOutOrderDetailMapper.updateStockOutOrderDetail(stockOutOrderDetail);
    }

    /**
     * 批量删除出库单详情
     *
     * @param ids 需要删除的出库单详情主键
     * @return 结果
     */
    @Override
    public int deleteStockOutOrderDetailByIds(Long[] ids) {
        return stockOutOrderDetailMapper.deleteStockOutOrderDetailByIds(ids);
    }

    /**
     * 删除出库单详情信息
     *
     * @param id 出库单详情主键
     * @return 结果
     */
    @Override
    public int deleteStockOutOrderDetailById(Long id) {
        return stockOutOrderDetailMapper.deleteStockOutOrderDetailById(id);
    }
}